#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: server.py
# Description: 
#   copy from lionaneesh@gmail.com at http://www.go4expert.com/forums/showthread.php?t=26371
# Edit History: 
#   2011-10-21    File created.
#===============================================================================
# Server  

import sys  
import socket  
import os  

host = '';  
SIZE = 512;  

try :  
    port = sys.argv[1];  

except :  
    port = 31337;  

try :  
    sockfd = socket.socket(socket.AF_INET , socket.SOCK_STREAM);  

except socket.error , e :  

    print "Error in creating socket : ",e ;  
    sys.exit(1);   

sockfd.setsockopt(socket.SOL_SOCKET , socket.SO_REUSEADDR , 1);  

try :  
    sockfd.bind((host,port));  

except socket.error , e :        
    print "Error in Binding : ",e; 
    sys.exit(1);  

print("\n\n======================================================"); 
print("-------- Server Listening on Port %d --------------" % port);  
print("======================================================\n\n"); 

try :  
    while 1 : # listen for connections  
        sockfd.listen(1);  
        clientsock , clientaddr = sockfd.accept();  
        print("\n\nGot Connection from " + str(clientaddr));  
        while 1 :  
            try :  
                cmd = clientsock.recv(SIZE);  
            except :  
                break;  
            pipe = os.popen(cmd);  
            rawOutput = pipe.readlines();  

            print(cmd);  

            if cmd == 'g2g': # close the connection and move on for others  
                print("\n-----------Connection Closed----------------");  
                clientsock.shutdown();  
                break;  
            try :  
                output = "";  
                # Parse the output from list to string  
                for data in rawOutput :  
                    output = output+data;  

                clientsock.send("Command Output :- \n"+output+"\r\n");  

            except socket.error , e :  

                print("\n-----------Connection Closed--------");  
                clientsock.close();  
                break;  
except  KeyboardInterrupt :  
    print("\n\n>>>> Server Terminated <<<<<\n");  
    print("==========================================================="); 
    print("\tThanks for using Simple-CMD");  
    print("\tEmail : lionaneesh@gmail.com");  
    print("============================================================");
